# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.contrib.localflavor.br.br_states import STATE_CHOICES
from bloom import BLOOM_CHOICES

class Endereco(models.Model):

    logradouro = models.CharField(max_length=100)
    bairro = models.CharField(max_length=100)
    cep = models.CharField(max_length=10)
    cidade = models.CharField(max_length=100)
    estado = models.CharField(max_length=2, choices=STATE_CHOICES)
    numero = models.CharField(max_length=10)
    complemento = models.CharField(max_length=100, blank=True)
    
    def __unicode__(self):
        return u'%s , %s' % (self.logradouro, self.numero)

class Professor(User):

    nome = models.CharField(max_length=100)
    dataNascimento = models.DateField()
    cpf = models.CharField(max_length=11, unique=True)
    tel_fixo = models.CharField(max_length=10)
    tel_cel = models.CharField(max_length=10, blank=True)

    endereco = models.ForeignKey(Endereco, related_name='endereco')
    doutorado = models.BooleanField(default=0)
    mestrado = models.BooleanField(default=0)
    espescialista = models.BooleanField(default=0)
     
    def __unicode__(self):
        return self.nome
    
class Disciplina(models.Model):

    nome = models.CharField(max_length=100, unique=True)

    def __unicode__(self):
        return self.nome

class Curso(models.Model):
    
    nome = models.CharField(max_length=50, unique=True)
    duracao = models.IntegerField()
    cargaHoraria = models.IntegerField()

    Coordenador = models.ForeignKey(Professor)

    disciplina = models.ManyToManyField(Disciplina, through='CursoDisciplina', related_name='disciplina')
    
    def __unicode__(self):
        return self.nome
    
class CompetenciaHabilidade(models.Model):

    descricao = models.CharField(max_length=50)
    bloom = models.CharField(max_length=20, choices=BLOOM_CHOICES)
    
    class Meta:
        unique_together=(('descricao','bloom'),)

    def __unicode__(self):
        return u'%s %s' % (BLOOM_CHOICES[int(self.bloom)-1][1], self.descricao)

class ObjetoAprendizagem(models.Model):

    objeto = models.FileField(upload_to="..\upload")
    
    def __unicode__(self):
        return self.objeto

class ConteudoProgramatico(models.Model):
    aula = models.CharField(max_length=100)
    descricao = models.CharField(max_length=500)
    dataPrevista = models.DateField()
    dataMinistrada = models.DateField()
    
    competenciahabilidade = models.ManyToManyField(CompetenciaHabilidade)
    objetoaprendizagem = models.ManyToManyField(ObjetoAprendizagem)
    
    def __unicode__(self):
        return self.descricao

class CursoDisciplina(models.Model):
    
    curso = models.ForeignKey(Curso, related_name='cursodisciplina_curso')
    disciplina = models.ForeignKey(Disciplina, related_name='cursodisciplina_disciplina')
    
    class Meta:
        unique_together=(('curso','disciplina'),)

    def __unicode__(self):
        return u'%s | %s' % (self.curso, self.disciplina)

class PlanoEnsino(models.Model):
    
    anoLetivo = models.IntegerField()
    semestre = models.IntegerField()
    turma = models.CharField(max_length=100)
    cargaHoraria = models.IntegerField()
    
    professor = models.ForeignKey(Professor, related_name='professor')
    curso_disciplina = models.ForeignKey(CursoDisciplina, related_name='cursodisciplina')
    conteudoprogramatico = models.ManyToManyField(ConteudoProgramatico, related_name='planoensino_conteudoprogramatico', blank=True, null=True, )
    
    disciplinarelacionada = models.ManyToManyField(Disciplina, through='DisciplinaRelacionada', related_name='disciplinarelacionada', blank=True, null=True)   
    competencia = models.ManyToManyField(CompetenciaHabilidade, related_name='competencia', blank=True, null=True)
    habilidade = models.ManyToManyField(CompetenciaHabilidade, related_name='habilidade', blank=True, null=True)
    
    class Meta:
        unique_together=(('anoLetivo','semestre','turma','professor','curso_disciplina'),)

    def __unicode__(self):
        return u'%s : %s' % (self.professor, self.curso_disciplina)

class DisciplinaRelacionada(models.Model):

    preRequisito = models.BooleanField(default=0)
    
    planoEnsino = models.ForeignKey(PlanoEnsino, related_name='diciplinarelacionada_planoensino')
    disciplina = models.ForeignKey(Disciplina, related_name='diciplinarelacionada_disciplina')
    
    class Meta:
        unique_together=(('planoEnsino','disciplina'),)

    def __unicode__(self):
        return u'%s : %s' % (self.planoEnsino, self.disciplina)

class PalavraChave(models.Model):
    
    palavraChave = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.palavraChave

class Classificacao(models.Model):
    
    caminhoTaxonomico = models.CharField(max_length=100)
    
    palavraChave = models.ManyToManyField(PalavraChave, related_name='palavrachave', blank=True, null=True)
    proposito = models.ManyToManyField(CompetenciaHabilidade, related_name='proposito', blank=True, null=True)
    
    def __unicode__(self):
        return self.caminhooTaxonomico    

class CicloVida(models.Model):
    
    versao = models.IntegerField()
    contribuidores = models.ManyToManyField(Professor, through='Contribuidor', related_name='contribuidores', blank=True, null=True)
    
    def __unicode__(self):
        return self.versao

class Contribuidor(models.Model):
    data = models.DateField()
    
    cicloVida = models.ForeignKey(CicloVida, related_name='contribuidor_ciclovida')
    identificacao = models.ForeignKey(Professor, related_name='contribuidor_identificacao')
    
    def __unicode__(self):
        return self.identificacao

class Tecnico(models.Model):
    
    formato = models.CharField(max_length=100)
    tamanho = models.CharField(max_length=100)
    local = models.FilePathField()
    
    def __unicode__(self):
        return self.local

class Educacional(models.Model):
    
    tipoRecurso = models.CharField(max_length=100)
    dificuldade = models.CharField(max_length=100)
    tempoAprendizado = models.TimeField()
    
    def __unicode__(self):
        return self.tipoRecurso

class Lom(models.Model):
    
    titulo = models.CharField(max_length=100)
    descricao = models.CharField(max_length=100)
    idioma = models.CharField(max_length=50)
    
    educacinal = models.ForeignKey(Educacional, related_name='educacional')
    tecnico = models.ForeignKey(Tecnico, related_name='tecnico')
    ciclovida = models.ForeignKey(CicloVida, related_name='ciclovida')
    classificacao =  models.ForeignKey(Classificacao, related_name='classificacao')
    
    relacao = models.ManyToManyField('self', through='Relacao', related_name='relacoes', symmetrical=False, blank=True, null=True)

    
    
    def __unicode__(self):
        return self.titulo
    
class Relacao(models.Model):
    
    tipoRelacao = models.CharField(max_length=100)
    identificadorRecurso =  models.ForeignKey(Lom, related_name='relacao_identificadorrecurso')
    identificadorRecursoRelacionado =  models.ForeignKey(Lom, related_name='relacao_recursorelacionado')
    
    def __unicode__(self):
        return self.tipoRelacao
    
    